<template>
	<view class="content font_family">
		<view class="top">
			<tkspbox :list='sp_msg' @jian='jian' @add='add' @select='select'></tkspbox>
		</view>
		<view class="type">
			<view class="item f_z_b f_j" @click="picker_open(1)">
				<view class="left">
					申请类型：
				</view>
				<view class="right f_j">
					<view class="text">
						{{tk_type_text}}
					</view>
					<image src="../../static/public/go_h.png" mode=""></image>
				</view>
			</view>
			<view class="item f_z_b f_j" @click="picker_open(2)">
				<view class="left">
					申请原因：
				</view>
				<view class="right f_j">
					<!-- no_text -->
					<view class="text ">
						{{query_data.apply?query_data.apply:'请选择'}}
					</view>
					<image src="../../static/public/go_h.png" mode=""></image>
				</view>
			</view>
		</view>
		<view class="xq_price">
			<view class="title">
				申请金额
			</view>
			<view class="ipt f_j">
				<text class="icon">￥</text>
				<u--input type='digit' placeholder="0.00" border="none" readonly v-model="query_data.amount"></u--input>
			</view>
		</view>
		<view class="xq_des">
			<view class="title">
				申请说明
			</view>
			<u--textarea v-model="query_data.content" placeholder="请写一写原因" border="none"></u--textarea>
			<view class="add_box f_j f_w">
				<view class="img_box" v-for="(item,index) in query_data.images" :key="index">
					<image @click="view_img(index,query_data.images)" class="img" :src="item" mode="aspectFill"></image>
					<view class="delete_btn" @click="delete_img(index)">
						<u-icon name="close-circle-fill" color="#FF3434" size="32rpx"></u-icon>
					</view>
				</view>

				<view class="add_btn f_d f_zj" @click="add_img" v-if="query_data.images.length<3">
					<image class="btn_img" src="../../static/order/add_img.png" mode=""></image>
					<text>图片</text>
				</view>
			</view>
		</view>
		<view class="btn_box">
			<view class="btn f_z" @click="sub">
				提交申请
			</view>
		</view>
		<u-picker :show="show" @close='show=false' @cancel='show=false' closeOnClickOverlay :columns="picker_data"
			keyName='name' confirmColor='#E1222A' @confirm='confirm'></u-picker>
	</view>
</template>

<script>
	import { mapState } from 'vuex'
	import { refund } from '@/api/user.js'
	export default {
		computed: {
			...mapState(['common'])
		},
		data() {
			return {
				status: '',
				tk_type_text: '我要退货退款',
				picker_data: '',
				picker_type: '',
				tk_type: [
					[
						{ name: '我要退货退款', type: 1 },
						{ name: '我要退款', type: 2 },
						{ name: '我要换货', type: 3 },
					]
				],
				see_all: false,
				box_height: '122rpx',
				show: false,
				sp_msg: '',

				query_data: {
					order_no: '',
					type: 1,
					apply: '',
					amount: '',
					content: '',
					images: [],
					detail: []
				}

			};
		},
		onLoad(e) {
			// let index = e.type
			this.query_data.order_no = e.order
			this.status = e.status
			// this.tk_type_text = this.tk_type[0][index - 1].name
			// this.query_data.type = this.tk_type[0][index - 1].type
			// this.sp_msg = JSON.parse(e.msg)
			this.sp_data(e.msg)
			if (this.status == 2) {
				this.tk_type = [
					[
						{ name: '我要退款', type: 2 },
					]
				]
				this.query_data.type = 2
				this.tk_type_text = '我要退款'
			}
		},
		methods: {
			// 处理商品数据
			sp_data(data) {
				let sp = JSON.parse(data)
				sp.forEach(item => {
					item.gx = false
					item.tk_count = item.sl_c
				})
				this.sp_msg = sp
			},
			picker_open(type) {
				if (type == 1) {
					this.picker_data = this.tk_type
					this.picker_type = 1
					this.show = true
				} else {
					let arr = []
					arr.push(this.common.reason)
					this.picker_data = arr
					this.picker_type = 2
					this.show = true
				}
			},
			all_click() {
				this.see_all = !this.see_all
				if (this.see_all) {
					this.box_height = 'auto'
				} else {
					this.box_height = '122rpx'
				}
			},
			add_img() {
				uni.chooseImage({
					count: 1,
					success: (res) => {
						console.log(res)
						if (res.errMsg == 'chooseImage:ok') {
							this.upload_img(res.tempFilePaths[0]).then(res => {
								if (res.code == 1) {
									this.query_data.images.push(res.data.fullurl)
								}
							})
						}
					}
				})
			},
			delete_img(index) {
				this.query_data.images.splice(index, 1)
			},
			confirm(e) {
				if (this.picker_type == 1) {
					this.query_data.type = e.value[0].type
					this.tk_type_text = e.value[0].name
				} else {
					this.query_data.apply = e.value[0]
					console.log(e.value[0])
				}
				this.show = false
			},
			sub() {
				// 加入需要退款的商品数据
				this.tk_sp_arr()
				if (this.kong(this.query_data)) {
					return
				} else {
					let data = uni.$u.deepClone(this.query_data)
					let arr = []
					data.images.forEach(item => {
						arr.push(this.removeDomainFromUrl(item))
					})
					data.images = arr
					console.log(data, '数据')
					refund(data).then(res => {
						if (res.code == 1) {
							// this.go(`/pages/order/order?index=4`)
							this.back()
						} else {
							uni.showToast({
								icon: 'none',
								title: res.msg
							})
						}
					})
				}
			},
			kong(data) {
				if (data.detail.length == 0) {
					uni.showToast({
						icon: 'none',
						title: '请选择需要退/换的商品'
					})
					return true
				}
				if (!data.apply) {
					uni.showToast({
						icon: 'none',
						title: '请选择申请原因'
					})
					return true
				}
				if (!data.amount) {
					uni.showToast({
						icon: 'none',
						title: '请输入退款金额'
					})
					return true
				}
				if (!data.content) {
					uni.showToast({
						icon: 'none',
						title: '请输入申请说明'
					})
					return true
				}
				return false
			},

			// 子组件事件
			jian(e) {
				if (this.sp_msg[e].tk_count > 1) {
					this.sp_msg[e].tk_count -= 1
				}
				this.js_price()
				this.$forceUpdate()
			},
			add(e) {
				if (this.sp_msg[e].tk_count < this.sp_msg[e].sl_c) {
					this.sp_msg[e].tk_count += 1
				}
				this.js_price()
				this.$forceUpdate()
			},
			select(e) {
				this.sp_msg[e].gx = !this.sp_msg[e].gx
				this.js_price()
				this.$forceUpdate()
			},
			tk_sp_arr() {
				let arr = []
				let price = 0
				this.sp_msg.forEach(item => {
					if (item.gx) {
						let obj = {}
						obj.detail_id = item.id
						obj.count = item.tk_count
						arr.push(obj)
						price += item.goods_price * item.tk_count
					}
				})
				this.query_data.amount = price.toFixed(1)
				this.query_data.detail = arr
			},
			// 计算价格
			js_price() {
				let price = 0
				this.sp_msg.forEach(item => {
					if (item.gx) {
						price += item.goods_price * item.tk_count
					}
				})
				if (price > 0) {
					this.query_data.amount = price.toFixed(1)
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		.top {
			background-color: #FFFFFF;
			padding: 30rpx;

			.sp_box {
				overflow: hidden;

				.sp_item {
					margin-bottom: 20rpx;

					image {
						width: 134rpx;
						height: 122rpx;
						border-radius: 8rpx 8rpx 8rpx 8rpx;
					}

					.msg {
						flex: 1;
						margin-left: 20rpx;

						.name {
							font-weight: 400;
							font-size: 28rpx;
							color: #666666;
						}

						.gg {
							font-weight: 400;
							font-size: 26rpx;
							color: #999999;
						}
					}

					.price {
						margin-left: 20rpx;
						font-weight: 400;
						font-size: 26rpx;
						color: #666666;

						.num {
							margin-top: 10rpx;
							align-self: flex-end;
						}
					}
				}
			}

			.all {
				font-weight: 250;
				font-size: 28rpx;
				color: #999999;

				image {
					margin-left: 10rpx;
					width: 30rpx;
					height: 30rpx;
				}
			}
		}

		.type {
			margin-top: 30rpx;
			background-color: #FFFFFF;
			padding: 30rpx;
			padding-top: 0rpx;

			.item {
				padding: 30rpx 0rpx;
				border-bottom: 1rpx solid #E4E7ED;

				.left {
					font-weight: 400;
					font-size: 28rpx;
					color: #232323;
				}

				.right {
					font-weight: 400;
					font-size: 28rpx;
					color: #232323;

					.no_text {
						font-weight: 400;
						font-size: 22rpx;
						color: #C0C4CC;
					}

					image {
						margin-left: 15rpx;
						width: 30rpx;
						height: 30rpx;
					}
				}
			}
		}

		.xq_price {
			margin-top: 30rpx;
			padding: 30rpx;
			background-color: #FFFFFF;

			.title {
				font-weight: 400;
				font-size: 28rpx;
				color: #3D3D3D;
			}

			.ipt {
				margin-top: 20rpx;
				font-weight: 400;
				font-size: 44rpx;
				color: #3D3D3D;

				.icon {
					margin-right: 20rpx;
				}
			}
		}

		.xq_des {
			margin-top: 30rpx;
			padding: 30rpx;
			background-color: #FFFFFF;

			.title {
				font-weight: 400;
				font-size: 28rpx;
				color: #3D3D3D;
				margin-bottom: 20rpx;
			}

			::v-deep .u-textarea {
				padding: 0rpx;
			}

			.add_box {
				margin-top: 20rpx;

				.img_box {
					margin-right: 15rpx;
					margin-bottom: 15rpx;
					position: relative;

					.delete_btn {
						position: absolute;
						top: 10rpx;
						right: 10rpx;
					}
				}

				.img {
					width: 120rpx;
					height: 106rpx;
					border-radius: 8rpx 8rpx 8rpx 8rpx;
				}

				.add_btn {
					margin-bottom: 15rpx;
					width: 120rpx;
					height: 106rpx;
					background: #F5F5F6;
					border-radius: 8rpx 8rpx 8rpx 8rpx;

					.btn_img {
						width: 48rpx;
						height: 48rpx;
					}

					text {
						font-weight: 400;
						font-size: 20rpx;
						color: #B9BABC;
					}
				}
			}
		}

		.btn_box {
			margin-top: 100rpx;
			padding: 30rpx;

			.btn {
				background: #EE4133;
				border-radius: 22rpx 22rpx 22rpx 22rpx;
				padding: 24rpx 0rpx;
				font-weight: 400;
				font-size: 28rpx;
				color: #FFFFFF;
			}
		}

		::v-deep .u-popup__content {
			border-radius: 40rpx 40rpx 0rpx 0rpx;
			overflow: hidden;
		}
	}
</style>